package org.dromara.house.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.house.domain.bo.ParkDistrictBo;
import org.dromara.house.domain.entity.ParkDistrict;
import org.dromara.house.mapper.ParkDistrictMapper;
import org.dromara.house.service.ParkDistrictService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 园区片区表 服务实现类
 * </p>
 *
 * @author LCH
 * @since 2025-08-26
 */
@Service
public class ParkDistrictServiceImpl extends ServiceImpl<ParkDistrictMapper, ParkDistrict> implements ParkDistrictService {

    @Override
    public TableDataInfo<ParkDistrict> selectPageList(ParkDistrictBo bo, PageQuery pageQuery) {
        Page<ParkDistrict> page = baseMapper.selectPageList(pageQuery.build(), this.buildQueryWrapper(bo));
        return TableDataInfo.build(page);
    }

    @Override
    public ParkDistrict selectById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertParkDistrict(ParkDistrict parkDistrict) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        parkDistrict.setCreateTime(new Date());
        parkDistrict.setCreateBy(loginUser.getUserId());
        parkDistrict.setDeleteStatus(0);
        return baseMapper.insert(parkDistrict);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDistrict(ParkDistrict parkDistrict) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        parkDistrict.setUpdateTime(new Date());
        parkDistrict.setUpdateBy(loginUser.getUserId());
        return baseMapper.updateById(parkDistrict);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Long[] ids) {
        for (Long id : ids) {
            ParkDistrict parkDistrict = baseMapper.selectById(id);
            parkDistrict.setDeleteStatus(1);
            baseMapper.updateById(parkDistrict);
        }
        return 1;
    }

    @Override
    public List<ParkDistrict> selectList() {
        return baseMapper.selectList(null);
    }

    private Wrapper<ParkDistrict> buildQueryWrapper(ParkDistrictBo bo) {
        LambdaQueryWrapper<ParkDistrict> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(bo.getDistrictName()), ParkDistrict::getDistrictName, bo.getDistrictName());
        wrapper.eq(ParkDistrict::getDeleteStatus, 0);
        wrapper.orderByDesc(ParkDistrict::getCreateTime);
        return wrapper;
    }
}
